﻿package com.winmastergames.screenManager {
	import com.winmastergames.lolgame.gameUtils;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.ui.Mouse;

	/**
	 * ...
	 * @author     Advanced Flash Engine in ActionScript 3.
	   Copyright (C) 2009  Dean Gardiner, Taylor Lodge

	   This program is free software: you can redistribute it and/or modify
	   it under the terms of the GNU General Public License as published by
	   the Free Software Foundation, either version 3 of the License, or
	   (at your option) any later version.

	   This program is distributed in the hope that it will be useful,
	   but WITHOUT ANY WARRANTY; without even the implied warranty of
	   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	   GNU General Public License for more details.

	   You should have received a copy of the GNU General Public License
	   along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */
	public class screenHandler {
		// TODO: Document This Class on Wiki.
		public var availableScreenArray:Array = new Array();
		public var currentScreens:Array = new Array();
		var screenArray:Array = new Array();
		var mainStage:Stage;
		var references:refHelper;

		public function screenHandler(stageReference:Stage, referenceHelper:refHelper){
			references = referenceHelper;
			mainStage = stageReference;
		}

		public function parseArrayString(inputString:String):Array {
			var tempInput:String = inputString;
			var splitInput:Array = tempInput.split(",");
			var outputArray:Array = new Array();
			for (var i:Number = 0; i < splitInput.length; i++){
				outputArray.push(splitInput[i]);
			}
			return outputArray;
		}

		public function makeScreen(screenBackground:MovieClip, gameStateName:String, screenIndex:int, enabledAssetString:String, disabledAssetString:String, buttonsArray:Array):screenObject {
			//Parse the Strings of Enabled and Disabled gameAssets.
			var enabledAssetsParsed:Array = new Array();
			enabledAssetsParsed = parseArrayString(enabledAssetString);
			var disabledAssetsParsed:Array = new Array();
			disabledAssetsParsed = parseArrayString(disabledAssetString);
			//Go through the enabled Objects Array and enable objects found.
			for (var i:Number = 0; i < enabledAssetsParsed.length; i++){
				switch (enabledAssetsParsed[i]){
					case "thisScreen":
						break;
					case "mouse":
						Mouse.show();
						break;
				}
			}
			//Go through the disabled Objects Array and disable objects found.
			for (var j:Number = 0; j < disabledAssetsParsed.length; j++){
				switch (disabledAssetsParsed[j]){
					case "level":
						references.r_level.visible = false;
						break;
					case "hud":
						references.r_hud.visible = false;
						break;
					case "player":
						references.r_player.visible = false;
						break;
					case "crosshair":
						references.r_crosshair.visible = false;
						break;
				}
			}
			//Create the Temporary Screen Object
			var tempScreenObject:screenObject = new screenObject(screenBackground, gameStateName, enabledAssetsParsed, disabledAssetsParsed, handleScreen, buttonsArray, mainStage);
			for (var l:Number = 0; l < buttonsArray.length; l++){
				var tempButton:buttonObject = buttonsArray[l];
				tempButton.buttonStage = tempScreenObject;
			}
			//Get Last Available Screen Size from Array
			var lastScreenArraySize:int = availableScreenArray.length;
			//push the Screen into the AvailableScreenArray.
			availableScreenArray.push(tempScreenObject);
			//Compare to make sure Screen was Added Sucessfully.
			if (availableScreenArray.length != lastScreenArraySize){
			} else {
			}
			//Return the Screen.
			return tempScreenObject;
		}

		public function handleScreen(e:Event){
		}

		public function createScreen(screenType:screenObject){
			gameUtils.setState(screenType.screenGameStateCode);
			currentScreens.push(screenType);
			mainStage.addChild(screenType.screenBackgroundAsset);
			for (var i:Number = 0; i < screenType.screenButtonsArray.length; i++){
				var tempButtonObject:buttonObject = screenType.screenButtonsArray[i];
				var tempButtonA:SimpleButton = tempButtonObject.buttonAsset;
				mainStage.addChild(tempButtonA);
				tempButtonA.x = tempButtonObject.buttonX;
				tempButtonA.y = tempButtonObject.buttonY;
			}
		}

		public function removeScreen(screenType:screenObject){
			mainStage.removeChild(screenType.screenBackgroundAsset);
			for (var i:Number = 0; i < screenType.screenButtonsArray.length; i++){
				var tempButtonObject:buttonObject = screenType.screenButtonsArray[i];
				var tempButtonA:SimpleButton = tempButtonObject.buttonAsset;
				mainStage.removeChild(tempButtonA);
			}
		}
	}
}